import json
import requests
from typing import Literal
from PyQt5.QtCore import QObject, QThread, pyqtSignal, QRunnable, QThreadPool
from PyQt5.QtWidgets import QWidget

from app.common.setting import COMMENT


class GetCommentThread(QThread):
    erroId = pyqtSignal()
    commentsInfo = pyqtSignal(dict)

    def __init__(self, resourceId: int = -1) -> None:
        super().__init__()

        self.resourceId = resourceId

        if self.resourceId == -1:
            self.erroId.emit()
            self.quit()

        self.jsons = {
            "type": "RESOURCE",
            "targetId": self.resourceId,
            "page": 1,
            "size": 9999,
        }

    def run(self):
        r = requests.get(COMMENT, params=self.jsons)
        result = json.loads(r.text)
        comments = result["data"]["comments"]
        for comment in comments:
            self.commentsInfo.emit(comment)
        
        self.quit()
